$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/columnsinfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '表ID', name: 'tableId', index: 'table_id', width: 80},
            {label: '列名', name: 'columnsName', index: 'columns_name', width: 80},
            {label: '列类型', name: 'columnsType', index: 'columns_type', width: 80},
            {label: '列长度', name: 'columnsLength', index: 'columns_length', width: 80},
            {label: '非空/空', name: 'isNullable', index: 'isNullable', width: 80,formatter:function (value,row) {
                var cname=""
                    console.log(value)
                if(value =="NO" || value =="t"){
                    cname="非空"
                }else if(value =="YES" || value =="f"){
                    cname="空"
                }else{
                    cname=""
                }
                    return cname;
                }},
            {label: '列注释', name: 'columnsZs', index: 'columns_zs', width: 80},
            {label: '填充率', name: 'tcl', index: 'tcl', width: 80},
            {label: '排序', name: 'sort', index: 'sort', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $("#connid").change(function () {
        var that = this;
        var dbid = $(this).val();
        var url = "sys/tableinfo/getTableByDbid/" + dbid;
        $.ajax({
            type: "GET",
            url: baseURL + url,
            success: function (r) {
                if (r.code === 0) {
                    $("#tableid").html('');
                    if (dbid == "0") {
                        $("#tableid").append('<option value="0">请选择表信息</option>');
                    } else {
                        for (var k in r.tables) {
                            var column = r.tables[k];
                            $("#tableid").append('<option value="' + column.id + '">' + column.tableName + '</option>');
                        }
                    }
                } else {
                    layer.alert("查询失败！");
                }
                return false;
            }
        });

    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            connid: "0",
            tableid: "0"
        },
        showList: true,
        title: null,
        dataSelect: [],
        columnsInfo: {},
        columnSelect: []
    }, created: function () {

        this.querySelect();

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.columnsInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        exportEXCEL: function () {

            var connid = $("#connid").val();
            var tableid = $("#tableid").val();
            var tableName = $("#tableid option:checked").text();
            if (connid != '0' && tableid != '0') {

                var url = "sys/excel/exportEXCEL/" + tableid+"/"+tableName;
                $.ajax({
                    type: "GET",
                    url: baseURL + url,
                    success: function (r) {
                        if (r.code === 0) {
                            layer.alert("导出成功！");
                        } else {
                            layer.alert("导出失败！");
                        }
                        return false;
                    }
                });

            } else {
                alert('0')
            }


        }


        ,
        querySelect: function () {
            var that = this;
            var url = "sys/databaseinfo/querySelect";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                success: function (r) {
                    if (r.code === 0) {
                        that.dataSelect = r.list;
                    } else {
                        layer.alert("查询失败！");
                    }
                    return false;
                }
            });
        },

        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.columnsInfo.id == null ? "sys/columnsinfo/save" : "sys/columnsinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.columnsInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/columnsinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/columnsinfo/info/" + id, function (r) {
                vm.columnsInfo = r.columnsInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'connid': vm.q.connid, 'tableid': $("#tableid option:selected").val()},

                page: page
            }).trigger("reloadGrid");
        }
    }
});